import { CycleTaskCancelType, CycleTaskCancelTypeList } from '@/pages/Task/utils';
import InfoModal from '@/pages/Task/components/InfoModal';
import { FILE_RENDER, REASON_RENDER, FORMAT_DATE, MINUTE_TO_WORK_STR } from './renderUtil';
import { fetchOperateDetails } from './util';

let cycleCancelCause = false;
// 取消详情弹框--------
const CANCEL_TITLE = '取消详情';
const WHOLE_COLUMNS = [
    {
        title: '是否已执行',
        dataIndex: 'executeFlag',
        require: true,
        render(value) {
            return value ? '是' : '否';
        }
    },
    {
        title: '开始时间',
        dataIndex: 'planStartTime',
        render: FORMAT_DATE
    },
    {
        title: '实际开始时间',
        dataIndex: 'actualStartTime',
        require: true,
        render: FORMAT_DATE
    },
    {
        title: '实际工时',
        dataIndex: 'actualWorkDuration',
        require: true,
        render: MINUTE_TO_WORK_STR
    },
    {
        title: '当前进度',
        dataIndex: 'taskProgress',
        render: value => `${value}%`
    },
    {
        title: '周期任务取消类型',
        dataIndex: 'cycleTaskCancelType',
        render: value => {
            const { text } = CycleTaskCancelTypeList.find(item => item.value === value) || {};
            if (!text) {
                return '';
            }
            return !cycleCancelCause && value === CycleTaskCancelType.ALL
                ? `${text}（已执行工时仅记录至当前任务）`
                : text;
        }
    },
    {
        title: '原因说明',
        dataIndex: 'operateReason',
        require: true,
        render: REASON_RENDER
    },
    {
        title: '附件',
        dataIndex: 'taskOperateFile',
        render: FILE_RENDER
    }
];
// 周期性任务-当前任务是否已执行为“否”时
const CYCLE_UNEXECUTED = ['executeFlag', 'cycleTaskCancelType', 'operateReason', 'taskOperateFile'];

// 非周期性任务-当前任务是否已执行为“否”时
const UNEXECUTED = ['executeFlag', 'operateReason', 'taskOperateFile'];
// 周期性任务-当前任务为被关联取消的周期任务时
const CYCLE_ASSOCIATED = ['cycleTaskCancelType', 'operateReason', 'taskOperateFile'];
// 周期性任务-当前任务是否已执行为“是”时
const CYCLE_EXECUTED = [
    'executeFlag',
    'planStartTime',
    'actualStartTime',
    'actualWorkDuration',
    'taskProgress',
    'cycleTaskCancelType',
    'operateReason',
    'taskOperateFile'
];

// 非周期性任务-当前任务是否已执行为“是”时
const EXECUTED = [
    'executeFlag',
    'planStartTime',
    'actualStartTime',
    'actualWorkDuration',
    'taskProgress',
    'operateReason',
    'taskOperateFile'
];
// 非周期性任务-发起任务审核不通过后的取消详情
const LAUNCH_AUDIT_NO_PASS = ['operateReason', 'taskOperateFile'];
// 退回时的取消详情
const SEND_BACK = LAUNCH_AUDIT_NO_PASS;
const getConfig = (colKeys, dataSource) => {
    const columns = colKeys.map(key => WHOLE_COLUMNS.find(item => item.dataIndex === key));
    return { title: CANCEL_TITLE, columns, dataSource };
};
const showCancelInfoModal = async (id, operateRecordId) => {
    const dataSource = await fetchOperateDetails(id, operateRecordId);
    cycleCancelCause = false;
    let colKeys;
    const {
        cycleTaskCancelAllRelateCause,
        executeFlag,
        cycleTaskFlag,
        launchAuditNoPassFlag,
        sendBackFlag
    } = dataSource;
    // 退回时的取消详情
    if (sendBackFlag) {
        colKeys = SEND_BACK;
    } else if (cycleTaskCancelAllRelateCause) {
        // 当前任务为被关联取消的周期任务时
        cycleCancelCause = true;
        colKeys = CYCLE_ASSOCIATED;
    } else if (launchAuditNoPassFlag) {
        // 发起任务审核不通过后的取消详情
        colKeys = LAUNCH_AUDIT_NO_PASS;
    } else if (!executeFlag) {
        // 当前任务是否已执行为“否”时
        colKeys = cycleTaskFlag ? CYCLE_UNEXECUTED : UNEXECUTED;
    } else if (executeFlag) {
        // 当前任务是否已执行为“是”时
        colKeys = cycleTaskFlag ? CYCLE_EXECUTED : EXECUTED;
    } else {
        throw new Error('数据异常~~~~');
    }

    return InfoModal.showCommonModal(getConfig(colKeys, dataSource));
};

export { showCancelInfoModal };
